using System;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;

namespace Scurvy.Media.Audio
{
    public class TimedCue
    {
        public float BeatsPerMinute;
        public float MeasureCount;
        public bool Playing;
        public string CueName;

        private SoundBank _bank;
        private double _timeStarted;
        private int _currentBeat;

        public TimedCue(SoundBank bank, string cuename, float bpm, float measureCount)
        {
            this._bank = bank;
            this.CueName = cuename;
            this.BeatsPerMinute = bpm;
            this.MeasureCount = measureCount;
        }

        public void Update(GameTime gameTime)
        {
            if (!this.Playing)// && this.Cue.IsPrepared)
            {
                this.Playing = true;
                _timeStarted = gameTime.TotalGameTime.TotalSeconds;
            }

            if (this.Playing)
            {
                float b = this.BeatsPerMinute / this.MeasureCount;
                float msBetweenBeats = (60 / b) * 1000;
                double timePassed = gameTime.TotalGameTime.TotalSeconds - _timeStarted;
                int beats = (int)(timePassed / (b / 60));
                if (beats != _currentBeat)
                {
                    _currentBeat = beats;
                    this._bank.PlayCue(this.CueName);
                }
            }
        }
    }
}
